package com.example.peibei.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.dingtao.common.bean.Evaluate;
import com.dingtao.common.bean.GetOrderDetail;
import com.dingtao.common.core.DataCall;
import com.dingtao.common.core.WDActivity;
import com.dingtao.common.core.exception.ApiException;
import com.dingtao.common.util.UIUtils;
import com.example.peibei.R;
import com.example.peibei.service.presenter.GetOrderDetailPresenter;
import com.example.peibei.service.presenter.SubmitEvaluatePresenter;
import com.example.peibei.ui.adapter.EvaluateAdapter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class EvaluateActivity extends WDActivity {
    @BindView(R.id.iv_head)
    ImageView iv_head;
    @BindView(R.id.tv_name)
    TextView tv_name;
    @BindView(R.id.tv_address)
    TextView tv_address;
    @BindView(R.id.iv_cate)
    TextView iv_cate;
    @BindView(R.id.rv_evaluate)
    RecyclerView rv_evaluate;
    @BindView(R.id.rg_evaluate)
    RadioGroup rg_evaluate;
    @BindView(R.id.et_evaluate)
    EditText et_evaluate;
    @BindView(R.id.ll_star)
    LinearLayout ll_star;

    private String token;
    private String consumeOrderId;
    private GetOrderDetailPresenter getOrderDetailPresenter;
    private List<Evaluate> evaluateList = new ArrayList<>();
    private EvaluateAdapter evaluateAdapter;
    private String type = "2";
    private SubmitEvaluatePresenter submitEvaluatePresenter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_evaluate;
    }

    @Override
    protected void initView() {
        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        token = bundle.getString("token");
        consumeOrderId = bundle.getString("consumeOrderId");
        getOrderDetailPresenter = new GetOrderDetailPresenter(new OrderPswCall());
        getOrderDetailPresenter.reqeust("Bearer " + token, consumeOrderId, "1");
        submitEvaluatePresenter = new SubmitEvaluatePresenter(new SubmitEvaluateCall());
        rv_evaluate.setLayoutManager(new GridLayoutManager(this, 3));
        evaluateAdapter = new EvaluateAdapter(evaluateList, EvaluateActivity.this);
        rv_evaluate.setAdapter(evaluateAdapter);
        evaluateList.add(new Evaluate("秋名山车神"));
        evaluateList.add(new Evaluate("幽默风趣"));
        evaluateList.add(new Evaluate("完美女神"));
        evaluateList.add(new Evaluate("话痨"));
        evaluateList.add(new Evaluate("城会玩"));
        evaluateList.add(new Evaluate("高手过招"));
        evaluateAdapter.notifyDataSetChanged();
        evaluateAdapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> a, @NonNull View view, int position) {
                Evaluate item = evaluateAdapter.getItem(position);
                et_evaluate.setText(item.getEvaluate());
            }
        });
        rg_evaluate.setOnCheckedChangeListener((group, checkedId) -> {
            switch (checkedId) {
                case R.id.rb_one:
                    type = "2";
                    setStarChecked(5);
                    break;
                case R.id.rb_two:
                    type = "1";
                    setStarChecked(3);
                    break;
                case R.id.rb_three:
                    type = "0";
                    setStarChecked(1);
                    break;
            }
        });
    }

    private void setStarChecked(int count) {
        for (int i = 0; i < 5; i++) {
            CheckBox checkBox = (CheckBox) ll_star.getChildAt(i);
            if (i < count) {
                checkBox.setChecked(true);
            } else {
                checkBox.setChecked(false);
            }
        }
    }

    @OnClick(R.id.iv_back)
    public void back() {
        finish();
    }

    @OnClick(R.id.tv_submit_evaluate)
    public void submit() {
        String s = et_evaluate.getText().toString();
        if (TextUtils.isEmpty(s)) {
            UIUtils.showToastSafe("评论内容不能为空哦~");
            return;
        }
        submitEvaluatePresenter.reqeust("Bearer " + token, s, consumeOrderId, type);
    }

    class OrderPswCall implements DataCall<GetOrderDetail> {

        @Override
        public void success(GetOrderDetail data, Object... args) {
            if (data != null) {
                Glide.with(EvaluateActivity.this).load(data.getImgUrl()).apply(RequestOptions.bitmapTransform(new CircleCrop())).into(iv_head);
                tv_address.setText(data.getArea());
                iv_cate.setText(data.getCategory());
                tv_name.setText(data.getNickName());
            }
        }

        @Override
        public void fail(ApiException e, Object... args) {
        }
    }

    class SubmitEvaluateCall implements DataCall<String> {

        @Override
        public void success(String data, Object... args) {
            UIUtils.showToastSafe("评价成功");
            finish();
        }

        @Override
        public void fail(ApiException e, Object... args) {
        }
    }

    @Override
    protected void destoryData() {

    }
}
